/*
 * @Author: ChunLai
 * @Date: 2023-03-17 14:05:48
 * @LastEditTime: 2025-08-19 10:00:30
 * @Description: 投保文件阅读处理
 * @FilePath: \02.bldinsure\src\store\modules\readings.js
 */
const state = [];
const getters = {
  lists: (state) => {
    return state;
  },
  // 必读html/imgs
  tabLists: (state) => {
    let res = [];
    res = state.filter((item) => ["html", "img"].includes(item.suffixType));
    return res;
  },
  // 必读pdf
  pdfMustReadLists: (state) => {
    let res = [];
    res = state.filter(
      (item) => item.suffixType === "pdf" && item.mustRead == "1"
    );
    return res;
  },
  // 非必读PDF
  pdfLists: (state) => {
    let res = [];
    res = state.filter(
      (item) => item.suffixType == "pdf" && item.mustRead != "1"
    );
    return res;
  },
  // 监听是否阅读完成
  htmlFinish: (state) => {
    let len = state.length;
    let res = false;
    for (let i = 0; i < len; i++) {
      const item = state[i];
      if (["html", "img"].includes(item.suffixType)) {
        if (!item.isFinish) {
          res = false;
          break;
        } else {
          res = true;
        }
      }
    }
    return res;
  },
  pdfFinish: (state) => {
    let len = state.length;
    let res = false;
    for (let i = 0; i < len; i++) {
      const item = state[i];
      if (item.suffixType === "pdf" && item.mustRead != "1") {
        if (!item.isFinish) {
          res = false;
          break;
        } else {
          res = true;
        }
      }
    }
    return res;
  },
  pdfMustReadFinish: (state) => {
    let len = state.length;
    let res = false;
    for (let i = 0; i < len; i++) {
      const item = state[i];
      if (item.suffixType === "pdf" && item.mustRead == "1") {
        if (!item.isFinish) {
          res = false;
          break;
        } else {
          res = true;
        }
      }
    }
    return res;
  },
  wholeFinish: (state, getters, rootState) => {
    let res = false;
    const len = state.length;
    if (len > 0) {
      for (let i = 0; i < len; i++) {
        if (!state[i].isFinish) {
          res = false;
          break;
        } else {
          res = true;
        }
      }
    } else {
      res = true;
    }
    // 条款阅读二次确认
    // if (res) {
    //   const clauseSedSure = JSON.parse(
    //     JSON.stringify(rootState.common.clauseSedSure)
    //   );
    //   res = clauseSedSure;
    // }
    return res;
  },
};
const mutations = {
  INIT_LIST(state, data) {
    if (data && data.length > 0) {
      for (let i = 0; i < data.length; i++) {
        state[i] = data[i];
      }
    } else {
      state = [];
    }
  },
  UPDATE_LIST_ITEM(state, payload) {
    Object.assign(state[payload.index], payload.data);
    state.splice(1, 0);
  },
  SET_DOCS(state, data) {
    state = data.splice(1, 0);
  },
};
const actions = {
  // 初始化条款配置
  initFilesList({ commit, dispatch, state, rootState }) {
    const appntMsg = rootState.insert.applicant;
    let demoFiles = rootState.page.comClauseList || [],
    tabFiles = rootState.page.tabClauses || [],
      showedRisks = rootState.risks.filter(
        (todo) => todo.ismain || todo.checked
      ),
      insureArea =
        appntMsg.province ||
        appntMsg.postalProvince ||
        appntMsg.homeProvince ||
        "";

    // 1.险种条款
    let riskClause = [];
    if (showedRisks.length > 0) {
      const list = showedRisks.filter(
        (ele) => ele.fileList && ele.fileList.length > 0
      );
      if (list.length > 0) {
        list.forEach((item) => {
          riskClause = [...riskClause, ...item.fileList];
        });
      }
    }
    // 2.保司公共配置文件
    if (demoFiles.length > 0) {
      riskClause = [...demoFiles, ...riskClause];
    }
    // 3.非pdf文件,各产品后台配置
    if (tabFiles.length > 0) {
      riskClause = [...tabFiles, ...riskClause];
    }
    // console.log('initFilesList', riskClause);
    const len = riskClause.length;
    // 3. 条款特殊操作
    for (let i = 0; i < len; i++) {
      const item = riskClause[i];
      // 不同区域配置不同版本的文件文本内容 [relate]
      if (item.relate) {
        const relateObj = JSON.parse(item.relate),
          areaArr = relateObj.province || [];
        if (areaArr.length > 0 && areaArr.includes(insureArea)) {
          item.fileName = relateObj.fileName;
          item.fileUrl = relateObj.fileUrl;
        }
      }
      if (!item.suffixType) {
        item.suffixType = "pdf";
      }
    }
    commit("INIT_LIST", JSON.parse(JSON.stringify(riskClause)));
    commit("SET_DOCS", state);
  },
  setWholeFinish({ commit, state }) {
    const len = state.length;
    for (let i = 0; i < len; i++) {
      const item = state[i];
      item.isFinish = true;
    }
    commit("SET_DOCS", state);
  },
  // 非必读直接阅读完
  setItemFinish({ commit, state }, data) {
    if (["pdf", "alink"].includes(data.type)) {
      const len = state.length;
      for (let i = 0; i < len; i++) {
        const item = state[i];
        if (item.suffixType === data.type) {
          if (data.type === "pdf" && item.mustRead != "1") {
            item.isFinish = true;
          } else if (data.type === "alink") {
            item.isFinish = true;
          }
        }
      }
    } else {
      const item = state.find((ele) => ele.id == data.index);
      if (item) {
        item.isFinish = true;
      }
    }
    commit("SET_DOCS", state);
  },
  // 必读PDF文档
  setItemPdfFinish({ commit, state }, pdfUrl) {
    const len = state.length;
    for (let i = 0; i < len; i++) {
      const item = state[i];
      if (item.fileUrl === pdfUrl) {
        item.isFinish = true;
      }
    }
    commit("SET_DOCS", state);
  },
  // setVali3date({ commit, dispatch, state, rootState }, data) {},
};
export default {
  namespaced: true,
  state,
  getters,
  mutations,
  actions,
};
